package com.xukehong.demo.controller;

import com.xukehong.demo.constant.Constants;
import com.xukehong.demo.util.FileUploadUtil;
import com.xukehong.demo.util.send.QiNiuYunUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.websocket.server.PathParam;
import java.util.Arrays;
import java.util.List;

import static com.xukehong.demo.constant.Constants.QNY_OUTER_CHAIN_URL;

/**
 * @Author: xkh
 * @Date: 2019/3/18 17:35
 * @Version 1.0
 */
@Controller
@RequestMapping("/util")
@Api(tags = "工具")
@Slf4j
public class UtilsController {
    @Value("${file.uploadFolder}")
    private String filePath;

    /**
     * 上传文件方法 文件位置->文件名->上传服务器
     */
    @ApiOperation("单个文件上传")
    @ResponseBody
    @PostMapping(value = "/upload")
    public String upload(MultipartFile file) throws Exception {
        //上传目录地址
        return FileUploadUtil.uploadUtil(file, filePath);
    }

    /**
     * 批量上传文件
     */
    @ApiOperation("多个文件上传")
    @PostMapping(value = "/uploads")
    @Transactional
    public String uploads(List<MultipartFile> files) {
        return FileUploadUtil.uploadsUtil(files, filePath);
    }

    /**
     * 表格导出
     * 常用组件
     * HSSFWorkbook excel的文档对象
     * HSSFSheet    excel的表单
     * HSSFRow      excel的行
     * HSSFCell     excel的格子单元
     * 样式 HSSFCellStyle  cell样式
     */
//    @ApiOperation("表格导出")
//    @RequestMapping(value = "/excel")
//    public void excel(HttpServletResponse response) throws IOException {
//        List<User> users = userService.findAll();
//        HSSFWorkbook workbook = new HSSFWorkbook();
//        HSSFSheet sheet = workbook.createSheet();
//        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
//        Date currentTime = new Date();
//        String dateString = format.format(currentTime);
//        String fileName = URLEncoder.encode("表格.xls", "utf-8");
//        workbook.setSheetName(0, "支付表");
//        int rowNum = 4;
//        //header表示excel表中第一行的表头
//        String[] headers = {"id", "用户名", "年龄"};
//
//        HSSFRow row = sheet.createRow(0);
//        row.createCell(0).setCellValue("导出时间");
//        row.createCell(1).setCellValue(dateString);
//
//        HSSFRow row3 = sheet.createRow(3);
//        for (int i = 0; i < headers.length; i++) {
//            HSSFCell cell = row3.createCell(i);
//            HSSFRichTextString text = new HSSFRichTextString(headers[i]);
//            cell.setCellValue(text);
//        }
//
//        //把查询出来的数据放入对应的列
//        for (User user : users) {
//            HSSFRow row1 = sheet.createRow(rowNum);
//            row1.createCell(0).setCellValue(rowNum - 4);
//            row1.createCell(1).setCellValue(user.getUsername());
//            row1.createCell(2).setCellValue(user.getPassword());
//            rowNum++;
//        }
//        response.setCharacterEncoding("UTF-8");
//        response.setContentType("application/force-download");
//        response.setHeader("Content-disposition", "attachment;filename=" + fileName);
//        response.setContentType("application/vnd.ms-excel;charset=utf-8");
//        response.flushBuffer();
//        workbook.write(response.getOutputStream());
//    }
//
//    /**
//     * API下单DEMO
//     */
//    @ApiOperation("API下单DEMO")
//    @RequestMapping(value = "/demo")
//    public String payDemo() {
//        return "payment/payDemo";
//    }
//
//    /**
//     * 导出txt
//     */
//    @GetMapping("/txt")
//    public void tex(HttpServletResponse response) throws IOException {
//        List list = userService.findAll();
//        response.setContentType("text/plain");// 一下两行关键的设置  
//        response.addHeader("Content-Disposition",
//                "attachment;filename=payOrder.txt");// filename指定默认的名字  
//        BufferedOutputStream buff = null;
//        StringBuffer write = new StringBuffer();
//        User user;
//        ServletOutputStream outSTr = null;
//        outSTr = response.getOutputStream();// 建立  
//        buff = new BufferedOutputStream(outSTr);
//        for (int i = 0; i < list.size(); i++) {
//            user = (User) list.get(i);
//            write.append(user);
//        }
//        buff.write(write.toString().getBytes("UTF-8"));
//        buff.flush();
//        buff.close();
//    }


    /**
     * 单个固定格式txt文件上传解析
     */
    @ApiOperation("单个固定格式text文件上传解析")
    @ResponseBody
    @PostMapping(value = "/txt")
    public String txt(MultipartFile file) {
        log.info(file.getName());
        //上传目录地址
        return "success";
    }

    @ApiOperation("七牛云文件上传")
    @ResponseBody
    @PostMapping("qnyUpload")
    public String qnyUpload(@PathParam("file") MultipartFile file){
        return QiNiuYunUtil.SimpleUpload(file);
    }


    @ApiOperation("七牛云根据文件名下载文件")
    @GetMapping("qnyDownload")
    @ResponseBody
    public String qnyDownload(String fileName){
        return String.format("%s/%s", Constants.QNY_OUTER_CHAIN_URL, fileName);
    }

}
